package cn.itsource.hrm.config;

import cn.itsource.hrm.domain.Permission;
import cn.itsource.hrm.handler.MyAuthenticationFailureHandler;
import cn.itsource.hrm.handler.MyAuthenticationSuccessHandler;
import cn.itsource.hrm.mapper.PermissionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.List;

@Configuration
//开启Security
@EnableWebSecurity
@Slf4j
//开启全局方法授权注册支持
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private MyAuthenticationSuccessHandler successHandler;

    @Autowired
    private MyAuthenticationFailureHandler failureHandler;

    @Autowired
    private PermissionMapper permissionMapper ;

    //密码编码器：不加密
    //security底层使用这个编码器来匹配密码
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
    
    //HttpSecurity :对Security的安全配置  ; 哪些放行，哪些不放心，什么资源需要什么权限才能访问
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        /**
        List<Permission> permissions = permissionMapper.selectList(null);
        permissions.forEach(permission -> {
            try {
                log.info("加载系统权限 {} - {}" , permission.getResource() ,permission.getSn());
                http.authorizeRequests()
                        .antMatchers(permission.getResource())
                        .hasAuthority(permission.getSn());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });**/

        //http.exceptionHandling().authenticationEntryPoint().accessDeniedHandler()

        http.authorizeRequests()                                //授权配置
                .antMatchers("/login","/loginUser/login").permitAll()  //匹配，登录路径放行
                .anyRequest().authenticated()                   //其他路径都要认证之后才能访问
                .and().formLogin()                              //允许表单登录
                .successHandler(successHandler) //认证成功处理器
                .failureHandler(failureHandler) //认证失败处理器
                //.successForwardUrl("/loginUser/loginSuccess")  // 设置登陆成功页
                .and().logout().permitAll()                    //登出(注销)路径放行 /logout
                .and().csrf().disable();                        //关闭跨域伪造检查
    }

    // AuthenticationManager :认证管理器 - 做登录的 ，
    // Oauth2的密码模式 需要用到AuthenticationManager
    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
}